<?php

namespace app\service;

use think\Db;
use think\Request;
use app\api\model\ConfigSystem as Config;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Vm\V20201229\VmClient;
use TencentCloud\Vm\V20201229\Models\CreateVideoModerationTaskRequest;
use TencentCloud\Vm\V20210922\Models\CancelTaskRequest;
use Tencent\TLSSigAPIv2;
use think\Log;


class VideoModerationTask
{
	public static function createTask($room_id, $order_id, $trtc_appid = '', $taskid = '')
	{
		if ($taskid) {
			// self::cancelTask($trtc_appid, $taskid);
		}
		try {
			$TLS_APPID = getenv('TLS_APPID');
			if ($TLS_APPID == $trtc_appid) {
				$SECRET_ID = getenv('SECRET_ID');
				$SECRET_KEY = getenv('SECRET_KEY');
				$TLS_KEY = getenv('TLS_KEY');
				$BIZ_TYPE = $taskid ? getenv('BIZ_TYPE_LAST') : getenv('BIZ_TYPE');
			} else {
				$TLS_APPID = getenv('RELANG_TLS_APPID');
				$SECRET_ID = getenv('RELANG_SECRET_ID');
				$SECRET_KEY = getenv('RELANG_SECRET_KEY');
				$TLS_KEY = getenv('RELANG_TLS_KEY');
				$BIZ_TYPE = getenv('RELANG_BIZ_TYPE');
			}
		    // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
		    // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
		    $cred = new Credential($SECRET_ID, $SECRET_KEY);
		    // 实例化一个http选项，可选的，没有特殊需求可以跳过
		    $httpProfile = new HttpProfile();
		    $httpProfile->setEndpoint("vm.tencentcloudapi.com");

		    // 实例化一个client选项，可选的，没有特殊需求可以跳过
		    $clientProfile = new ClientProfile();
		    $clientProfile->setHttpProfile($httpProfile);
		    // 实例化要请求产品的client对象,clientProfile是可选的
		    $client = new VmClient($cred, "", $clientProfile);

		    // 实例化一个请求对象,每个接口都会对应一个request对象
		    $req = new CreateVideoModerationTaskRequest();

		    $user_id = ($taskid?990000000:90000000) + $order_id;
	        $api = new TLSSigAPIv2($TLS_APPID, $TLS_KEY);
	        $user_sig = $api->genSig($user_id);
		    
		    $params = array(
		    	'Type'=>'LIVE_VIDEO',
		    	'Action'=>'CreateVideoModerationTask',
		    	'Version'=>'2020-12-29',
		    	'Tasks'=>array(array(
		    		'DataId'=>'jhinr'.$room_id.'t'.time(),
		    		'Name'=>'视频通话鉴黄',
		    		'Input'=>array(
		    			'Type'=>'URL',
				    	'Url'=>'trtc://trtc.tencentcloudapi.com/moderation?mix=false&sdk_app_id='.$TLS_APPID.'&room_id='.$room_id.'&user_id='.$user_id.'&user_sig='.$user_sig
		    		)
		    	)),
		    	'BizType'=>$BIZ_TYPE,
		    	'CallbackUrl'=>'http://'.getenv('APP_URL') . '/api/Articlecheck/vidoenotify'
		    );
		    $req->fromJsonString(json_encode($params));

		    // 返回的resp是一个CreateVideoModerationTaskResponse的实例，与请求对象对应
		    $resp = $client->CreateVideoModerationTask($req);

		    // 输出json格式的字符串回包
		    return json_decode($resp->toJsonString(), true);
		}
		catch(TencentCloudSDKException $e) {
		    // echo $e;
		    Log::write('创建视频审核失败'.$room_id.':'.$order_id.':'.$trtc_appid.':'.$e.':' . $e->getMessage(),'error');
		    return false;
		}
	}

	public static function cancelTask($trtc_appid = '', $taskid = '')
	{
		$TLS_APPID = getenv('TLS_APPID');
		if ($TLS_APPID == $trtc_appid) {
			$SECRET_ID = getenv('SECRET_ID');
			$SECRET_KEY = getenv('SECRET_KEY');
		} else {
			$SECRET_ID = getenv('RELANG_SECRET_ID');
			$SECRET_KEY = getenv('RELANG_SECRET_KEY');
		}
		try {
		    // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
		    // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
		    // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
		    $cred = new Credential($SECRET_ID, $SECRET_KEY);
		    // 实例化一个http选项，可选的，没有特殊需求可以跳过
		    $httpProfile = new HttpProfile();
		    $httpProfile->setEndpoint("vm.tencentcloudapi.com");

		    // 实例化一个client选项，可选的，没有特殊需求可以跳过
		    $clientProfile = new ClientProfile();
		    $clientProfile->setHttpProfile($httpProfile);
		    // 实例化要请求产品的client对象,clientProfile是可选的
		    $client = new VmClient($cred, "", $clientProfile);

		    // 实例化一个请求对象,每个接口都会对应一个request对象
		    $req = new CancelTaskRequest();

		    $params = array(
		        "TaskId" => $taskid
		    );
		    $req->fromJsonString(json_encode($params));

		    // 返回的resp是一个CancelTaskResponse的实例，与请求对象对应
		    $resp = $client->CancelTask($req);

		    // 输出json格式的字符串回包
		    // print_r($resp->toJsonString());
		}
		catch(TencentCloudSDKException $e) {
		    // echo $e;
		}
	}
}